/* jslint browser: true, white: true, onevar: false, undef: true, nomen: true, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, newcap: true, immed: true, indent: 2 */

var APP = function () {
  var http;
  var getQuotes;

  var showChange = function (change) {
    var s = change.substr(1);
    if (change.charAt(0) === "+") {
      return s.fontcolor("#00FF00");
    }
    if (change.charAt(0) === "-") {
      return s.fontcolor("#FF0000");
    }
    return s;
  };

  var addCommas = function (nStr) {
    nStr += '';
    var x = nStr.split('.');
    var x1 = x[0];
    var x2 = x.length > 1 ? '.' + x[1] : '';
    var rgx = /(\d+)(\d{3})/;
    while (rgx.test(x1)) {
      x1 = x1.replace(rgx, '$1' + ',' + '$2');
    }
    return x1 + x2;
  };

  var showStock = function (stock, name) {
    try {
      document.getElementById(name + "_price").innerHTML = addCommas(stock.l_cur);
      document.getElementById(name + "_change").innerHTML = showChange(stock.c);
    }
    catch (e) {
      document.getElementById(name + "_price").innerHTML = "???";
    }
  };

  var updateTimer = (function () {
    var id = -1;
    return function (mode) {
      if (id !== -1) { clearInterval(id); }
      var interval = 15 * 1000 * ((mode !== "fast") ? 60 : 1);
      id = setInterval(getQuotes, interval);
    };
  })();

  var stripQuotes = function (item) {
    return item.replace(/"/g, '');
  };

  var showDate = function (stock) {
    var date;
    try {
      date = stripQuotes(stock.lt);
    }
    catch (e) {
      date = "???";
    }
    document.getElementById("date").innerHTML = date;
  };

  var gotQuotes = function () {
    try {
      if (http && http.readyState === 4) {
        var data = JSON.parse(http.responseText.substring(3));
        showDate(data[0]);
        showStock(data[0], "dji");
        showStock(data[1], "nasdaq");
        showStock(data[2], "sp");
        http = null;
        data = null;
        updateTimer();
      }
    }
    catch (e) {
      http = null;
      updateTimer("fast");
    }
  };

  getQuotes = function () {
    try {
      http = new XMLHttpRequest();
      http.onreadystatechange = gotQuotes;
      http.open("GET", "http://www.google.com/finance/info?client=ig&q=INDEXDJX:.DJI,INDEXNASDAQ:.IXIC,INDEXSP:.INX", true);
      http.send(null);
    }
    catch (e) {
      http = null;
      updateTimer("fast");
    }
  };

  var wheel = function () {
    var delta = window.event.wheelDelta;

    if (delta !== 0) {
      var colors = ['black.png', 'dark.png', 'gray.png', 'green.png', 'blue.png'];
      for (var i = 0; i < colors.length; ++i) {
        var background = System.Gadget.background;
        if (background === colors[i]) {
          background = colors[(delta > 0 ? ++i : --i + colors.length) % colors.length];
          System.Gadget.Settings.writeString("background", background);
          System.Gadget.background = background;
          break;
        }
      }
    }
  };

  document.getElementById("content").attachEvent("onmousewheel", wheel);
  var background = System.Gadget.Settings.readString("background");
  if (background === "") { background = "blue.png"; }
  System.Gadget.background = background;
  getQuotes();
};
